/*
 *  Date: 2011-11-6 下午03:52:22
 *  Copyright (c) 2011 asiainfo-linkage
 */
package com.gjframe.example.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gjframe.commen.service.ServiceFactory;
import com.gjframe.commen.web.Controller;
import com.gjframe.commen.web.view.JsonView;
import com.gjframe.example.bean.BasePerson;
import com.gjframe.example.service.PersonService;
import com.gjframe.example.utils.CookieUtils;

public class PersonAction extends Controller {

    private Log log = LogFactory.getLog(PersonAction.class);

    private final String EDIT = "2";

    private PersonService personService = ServiceFactory.getService(PersonService.class);

    public void index() throws Exception {
        log.debug("go index page");
    }

    public void view() throws Exception {
        request.setAttribute("person", personService);
    }

    public void list(BasePerson person, Integer pageNo, Integer pageSize) throws Exception {
        if(pageNo==null){
            pageNo=1;
        }
        request.setAttribute("pag", personService.findList(person, pageNo, CookieUtils.getPageSize(request)));
    }

    public void edit(Integer id, String flag) throws Exception {
        if (id != null && id != 0 && EDIT.equals(flag)) {
            request.setAttribute("person", personService.findById(id));
        }
    }

    public JsonView save(BasePerson person,String flag) throws Exception {
        if(EDIT.equals(flag)){
            personService.update(person);
        }else{
            personService.save(person);
        }
        return new JsonView("flag:'S'");
    }
    
    public JsonView del(Integer id) throws Exception {
        personService.delete(id);
        return new JsonView("flag:'S'");
    }

}
